package com.application.Activity;



import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.application.Adapter.ListChapterAdapter;
import com.application.Adapter.ListChapterAdapter.ViewChapter;
import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.DTO.ChapterDTO;
import com.application.DataProvider.ChapterDAO;


public class FrmChapter extends Activity implements OnItemClickListener{

	private static ArrayList<ViewChapter> _arrayWork;
	private static ListChapterAdapter _arrayAdapter;
	private static Context  _cont = null;
	private static ListView _listChapter;	
	private static ArrayList<ChapterDTO> _chapList = new ArrayList<ChapterDTO>();
	private static int _idSub = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_chapter);
		NetworkController.init_runnableUpdateChapter();
		_cont = this;
		_arrayWork = new ArrayList<ViewChapter>();
		_listChapter = (ListView)findViewById(R.id.frmchapter_lvListChapter) ;

		Log.d("PEERDROID","Da send toi FrmChapter");
		ShowChapterList();


		//		TextView tvUserName = (TextView)findViewById(R.id.chapter_tvUsername);
		//		tvUserName.setText(NetworkController.get_userName());
		//		tvUserName.setOnClickListener(new OnClickListener() {
		//
		//			@Override
		//			public void onClick(View arg0) {
		//				// TODO Auto-generated method stub
		//				Intent iIntent = new Intent(FrmChapter.this,FrmConversation.class);
		//				startActivity(iIntent);
		//			}
		//		});

		//Su kien click item on listview
		_listChapter.setOnItemClickListener( this);
	}
 
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart chapter");
	//	ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
	//	ActivityController.setPeerActivity(this);
	}

	@Override
	public void onPause(){
		super.onPause();
		//ActivityController.setPeerActivity(this);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			ActivityController.handler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					FrmSubject.updateListAdapter();
				}
			});
		}

		return super.onKeyDown(keyCode, event);	
	}


	public static void updateListAdapter()
	{
		_arrayWork = new ArrayList<ListChapterAdapter.ViewChapter>();
		_chapList.clear();
		ChapterDAO ctDAO = new ChapterDAO(_cont);
		_chapList= ctDAO.getChapterList(_idSub);

		try {
			int n = _chapList.size();
			for(int i=0;i<n;i++)
			{
				String sName = _chapList.get(i).get_Name();
				Log.d("PEERDROID", "sName: " + sName);
				String sCont = _chapList.get(i).get_ContentAbstract();
				Log.d("PEERDROID", "sCont " + sCont);
				int idChap = _chapList.get(i).get_ID();
				Log.d("PEERDROID", "idchap" + idChap);
				boolean status = ctDAO.getStatus(idChap);
				Log.d("PEERDROID", "status" + status);
				ViewChapter view=new ViewChapter(sName,sCont,idChap, status);
				_arrayWork.add(view);
			}
			Log.d("PEERDROID", "arraywork: " + _arrayWork.get(0).sViewContent);
			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}
	public static void setListAdapter() {

		_arrayAdapter=new ListChapterAdapter(_cont,R.layout.listrow_listchapter,_arrayWork);
		_listChapter.setAdapter(_arrayAdapter);

	}
	//--- Show danh sach bai hoc cua 1 mon hoc cu the
	private void ShowChapterList(){

		Log.d("PEERDROID","Da vao ShowChapter");
		_chapList.clear();
		Bundle bBundle = this.getIntent().getExtras();		
		ChapterDAO ctDAO = new ChapterDAO(this);
		set_idSub(Integer.valueOf(bBundle.getString("id")));
		_chapList= ctDAO.getChapterList(get_idSub());
		try {
			int n = _chapList.size();
			for(int i=0;i<n;i++)
			{
				ViewChapter view;
				String sName = _chapList.get(i).get_Name();
				String sCont = _chapList.get(i).get_ContentAbstract();
				int idChap = _chapList.get(i).get_ID();
				boolean status = ctDAO.getStatus(idChap);
				view=new ViewChapter(sName,sCont,idChap, status);
				_arrayWork.add(view);
			}

			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}

	//--- Xu ly su kien ItemClick tren listview
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int iPos, long arg3) {
		// TODO Auto-generated method stub
		//Log.d("PEERDROID","Da vao ham Onclick");
		showFrmPart(iPos);		
	}

	private void showFrmPart(int iPos) {

		Log.d("PEERDROID","Chuan bi Show part");

		String idChap = String.valueOf(_chapList.get(iPos).get_ID());
		ChapterDAO cDao = new ChapterDAO(null);
		cDao.updateStatusChapter(false, Integer.valueOf(idChap));
		//Send id
		Intent iIntent = new Intent(FrmChapter.this,FrmPart.class);
		Bundle bBun = new Bundle();
		bBun.putString("id", idChap);
		iIntent.putExtras(bBun);
		StackActivity activityStack = (StackActivity) getParent();
		activityStack.push("FrmPart", iIntent);
	}

	public static void set_cont(Context _cont) {
		FrmChapter._cont = _cont;
	}

	public static Context get_cont() {
		return _cont;
	}

	public static void set_idSub(int _idSub) {
		FrmChapter._idSub = _idSub;
	}

	public static int get_idSub() {
		return _idSub;
	}
}
